(function () {

    app.controller("InfoCtrl", ['$scope', '$rootScope', function ($scope, $rootScope) {
        $scope.openModify = function (id) {
            $rootScope.$broadcast('modifyWindow', id);
        };
        $scope.openDetail = function (id) {
            $rootScope.$broadcast('showDetail', id);
        };
    }]);

    app.controller("detailCtrl", ['$scope', '$rootScope', 'userService', function ($scope, $rootScope, userService) {

        $rootScope.$on("showDetail", function (event, data) {
            userService.findById(data).done(function (model) {
                $scope.updateUser = model.data;
                $scope.$apply();
                $scope.details.open();
            });

        })
    }]);

    app.controller("modifyCtrl", ['$scope', '$rootScope', 'userService', function ($scope, $rootScope, userService) {

        function check() {
            if($scope.modifyPw.password.length < 6){
                kendo.factory.notifyInfo("新密码最小6位");
                return false;
            }

            if (!$scope.modifyPw.password) {
                kendo.factory.notifyInfo("新密码不能空");
                return false;
            }
            else if ($scope.modifyPw.password != $scope.modifyPw.password1) {
                kendo.factory.notifyInfo("两次输入的密码不一致");
                return false;
            } else {
                return true;
            }
        }

        $scope.actions = [
            {text: "取消"},
            {
                text: "确认修改",
                primary: true,
                action: function (e) {
                    if ($scope.validator.validate()) {
                        if (check()) {
                            userService.modifypw($scope.modifyPw)
                                .done(function (result) {
                                    if (result.success) {
                                        $scope.modifyDailog.close();
                                        kendo.factory.notifySuccess("修改成功");
                                    } else {
                                        kendo.factory.notifyError(result.message);
                                    }
                                });
                            return false;
                        } else {
                            return false
                        }
                    }

                }
            }
        ];


        $rootScope.$on("modifyWindow", function (event, data) {
            userService.findById(data).done(function (model) {
                document.getElementById('user').reset();
                $scope.modifyPw = model.data;
                $('#username').val($scope.modifyPw.username);
                $scope.modifyPw.password = "";
                $scope.$apply();
                $scope.modifyDailog.open();
            });
        })
    }]);
})();